package com.fsc.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.fsc.entity.domain.StuHomeworkInfo;


import com.fsc.entity.vo.HomeworkInfoVO;
import com.fsc.entity.vo.StudentCompletionVO;
import com.fsc.mapper.HomeworkInfoMapper;
import com.fsc.mapper.StuHomeworkInfoMapper;

import com.fsc.service.StuHomeworkInfoService;

import com.fsc.util.OssUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Slf4j
@Service
public class StuHomeworkInfoServiceImpl extends ServiceImpl<StuHomeworkInfoMapper, StuHomeworkInfo> implements StuHomeworkInfoService {



	@Autowired
	private HomeworkInfoMapper homeworkInfoMapper;

	private static final String endpoint = "oss-cn-guangzhou.aliyuncs.com";
	private static final String bucketName = "su-bucket666";
	/**
	 * 老师查看某次作业的学生的完成情况
	 *
	 * @param homeworkId
	 * @param teacherId
	 * @return
	 */
	@Override
	public List<StudentCompletionVO> getStuHomeworkInfosByHomeworkId(Integer homeworkId, Integer teacherId) {
		// Get all StuHomeworkInfo by homeworkId from the database
		List<StudentCompletionVO> stuHomeworkInfos = this.baseMapper.selectByHomeworkID(homeworkId,teacherId);
		return stuHomeworkInfos;
	}

	/**
	 * 老师根据学生id获取某位学生的作业内容详情
	 * @param studentId
	 * @param homeworkId
	 * @return
	 */
	@Override
	public StudentCompletionVO getHomeworkDetailsByStudentId(Integer studentId, Integer homeworkId) {

		StudentCompletionVO stuHomeworkInfos = this.baseMapper.selectByStudentID(studentId,homeworkId);
		return stuHomeworkInfos;
	}

	/**
	 * 老师修改作业
	 * @param id
	 * @param score
	 * @return
	 */
	@Override
	public boolean updateHomeworkScore(Integer id, BigDecimal score) {
		try {
			// 直接根据 StuHomeworkInfoId 查找对应的记录
			StuHomeworkInfo homeworkInfo = this.baseMapper.selectById(id);
			if (homeworkInfo != null) {
				// 更新成绩
				homeworkInfo.setScore(score);

				return this.baseMapper.updateById(homeworkInfo) > 0;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	/**
	 * 老师布置作业
	 * @param student
	 * @param homeworkId
	 */
	@Override
	public void addHomework(List<Integer> student, Integer homeworkId) {
		for (int i = 0; i < student.size(); i++) {
			this.baseMapper.save(student.get(i),homeworkId);
		}
	}

	/*
	学生查看自己的作业
	 */
	@Override
	public List<HomeworkInfoVO> getHomeworkByStudentId(Integer loginId) {
		List<Integer> homeworkids= this.baseMapper.getHomeworkIdByStudentID(loginId);
		List<HomeworkInfoVO> homeworkInfoVOS = new ArrayList<>();
		for (int i = 0; i < homeworkids.size(); i++) {
			homeworkInfoVOS.add(homeworkInfoMapper.getHomeworkInfoVOsByhomeworkId(homeworkids.get(i)));
		}
		return homeworkInfoVOS;
	}




	@Override
	public void submitHomework(Integer homeworkId, String text, MultipartFile image, Integer studentId) throws IOException {
		// 生成文件名
		String fileName = (image != null) ? generateUniqueFileName(image.getOriginalFilename()) : null;
		OssUtil.uploadImage(image.getBytes(), fileName);
		// 返回完整URL
		String fullUrl = "https://" + bucketName + "." + endpoint + "/" + fileName;
		// 只保存路径部分
		String imageUrl = fullUrl.substring(fullUrl.indexOf("image/"));
		LocalDateTime now = LocalDateTime.now();
		this.baseMapper.updateHomework(imageUrl,text,homeworkId,studentId,now);
	}

	@Override
	public void submitHomeworkOnlyText(Integer homeworkId, String text, Integer studentId) {
		LocalDateTime now = LocalDateTime.now();
		this.baseMapper.updateHomework(null,text,homeworkId,studentId,now);
	}

	@Override
	public void deleteById(Integer homeworkId) {
		this.baseMapper.deleteByHomeworkId(homeworkId);
	}


	private String generateUniqueFileName(String originalFilename) {
		String uuid = UUID.randomUUID().toString().replaceAll("-", "");
		return "image/" + uuid + originalFilename;
	}
}